//
//  YTDetailTipCell.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/7/17.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTDetailTipCell.h"
#import "YTHDLikeCell.h"
#import "YTArticleTipCell.h"

NSString *YTDLikeCellID = @"YTDLikeCellID";
NSString *YTArticleTipCellID = @"YTArticleTipCellID";
@interface YTDetailTipCell()<UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic ,strong)UICollectionView *collectionView;
@property(nonatomic ,strong)UICollectionViewFlowLayout *layout;

@end
@implementation YTDetailTipCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.layout = [[UICollectionViewFlowLayout alloc] init];
        self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:self.layout];
        self.collectionView.dataSource = self;
        self.collectionView.delegate = self;
        self.layout.footerReferenceSize = CGSizeMake(kScreenWidth, 80);
        
        self.collectionView.backgroundColor = UIColor.whiteColor;
        self.collectionView.scrollEnabled = false;
        
        
        // [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellID"];
        // [self.collectionView registerNib:[UINib nibWithNibName:@"YTHDLikeCell" bundle:nil] forCellWithReuseIdentifier:YTDLikeCellID];
        [self.collectionView registerNib:[UINib nibWithNibName:@"YTHDLikeCell" bundle:nil]
              forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView"];
        [self.collectionView registerNib:[UINib nibWithNibName:@"YTArticleTipCell" bundle:nil] forCellWithReuseIdentifier:YTArticleTipCellID];
        [self.contentView addSubview:self.collectionView];
    }

    return self;
}



- (void)setModel:(YTHomeDetailModel *)model
{
    _model = model;
    
    [self.collectionView reloadData];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
 
    self.collectionView.frame = self.bounds;
}

#pragma mark: - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.model.newsTerms.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:YTArticleTipCellID forIndexPath:indexPath];

    [(YTArticleTipCell *) cell setTipName:self.model.newsTerms[indexPath.row][@"name"]];

    
    return cell;
}

#pragma mark: - UICollectionViewDelegate
- (CGSize)collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:( NSIndexPath *)indexPath
{
    CGFloat height = (self.model.newsTerms.count > 0) ? 40 : 0;

    return CGSizeMake(kScreenWidth / 6, height);

}

- (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    YTHDLikeCell *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView" forIndexPath:indexPath];
    
    footerView.model = self.model;
    return footerView;
    
//    if (indexPath.row == self.model.newsTerms.count) {
//       // footerView.model = self.model;
//        return footerView;
//    }else{
//        return [[UICollectionReusableView alloc] init];
//    }
    
}

-(CGFloat)minimumInteritemSpacing
{
    return 0;
}
-(CGFloat)minimumLineSpacing
{
    return 0;
}



@end
